<?php
//目的：报价负责人技术标书文件提交，pdf；
session_start();
//包含头
include '../PHP/head.php';
?>
<?php
//包含bootstrap的顶端按钮
include '../PHP/menu.php';
?>


<br />
<br />
<p class="text-center lead">技术标书文件上传</p>
<?php
//接收task_id后上传底价文件，并修改task标记
if(isset($_GET['project_id'])) {
	//查询这个任务的，这个人的，type是标书的记录
	//查询编号信息
	require_once ('../FNS/db_fns.php');
	
	$project_id = $_GET['project_id'];
	$task_id = $_GET['task_id'];
	$subject = $_GET['subject'];
	
	//数据过滤和处理

	if (! get_magic_quotes_gpc()) {
	$project_id=addslashes ($project_id);
	}
	
	$db = db_connect();
	$query="select projects.code as project_code, projects.name as project_name, projects.sale as sale_id, projects.entrust_date, users.name as sale_name from projects inner join users on projects.sale = users.id where projects.id = '".$project_id."'";
	
	$result=$db->query ($query);
	$num_result=$result->num_rows;
	
	//拿出数据
	$row=$result->fetch_assoc ();
	$project_code = stripslashes ($row['project_code']);
	$project_name = stripslashes ($row['project_name']);
	$sale_name = stripslashes ($row['sale_name']);
	$sale_id = stripslashes ($row['sale_id']);

	
	$result->free();
	$db->close();
	
	//列出编号基本信息
	echo "
		<br />
		<div class='row'>
			<div class='col-md-2'><p class='text-right'>报价编号：</p></div>
			<div class='col-md-2'><p>".$project_code."</p></div>
			<div class='col-md-2'><p class='text-right'>项目名称：</p></div>
			<div class='col-md-4'><p>".$project_name."</p></div>
			<div class='col-md-1'><p class='text-right'>销售：</p></div>
			<div class='col-md-1'><p>".$sale_name."</p></div>
		</div>
		<br />
	
	";
}	
?>



<?php
	
	
if (isset($_POST['project_id'])) {
		
	require_once ('../FNS/db_fns.php');
	require_once ('../FNS/ftp_class2.php');
	
	$project_id = trim($_POST['project_id']);
	$task_id = trim($_POST['task_id']);
	$subject = trim($_POST['subject']);
			
	//数据过滤和处理
	//加引号避免输入数据库出错
	if (! get_magic_quotes_gpc()) {	
	$project_id = addslashes ($project_id);
	$task_id = addslashes ($task_id);
	$subject = addslashes ($subject);
	}
	
	$issue_date = date('Y-m-d');
	
	//连接数据库，基本项目信息
	$db = db_connect();
	
	
	$query="select projects.code as project_code, projects.name as project_name, projects.sale as sale_id, projects.entrust_date, users.name as sale_name from projects inner join users on projects.sale = users.id where projects.id = '".$project_id."'";
	
	$result=$db->query ($query);
	$num_result=$result->num_rows;
	
	//拿出数据
	$row=$result->fetch_assoc ();
	$project_code = stripslashes ($row['project_code']);
	$project_name = stripslashes ($row['project_name']);
	$sale_name = stripslashes ($row['sale_name']);
	$sale_id = stripslashes ($row['sale_id']);

	$result->free();
	
	//文件名
	$file_name = $project_code."_".$subject."_proposal";
	
	
	//还需要版本信息，和提交人id
	$query="select * from files where name LIKE '".$file_name."%'";
	$rev = 0;
	
	$result=$db->query ($query);
	if (!$result) {
		echo "<p>版本信息搜索失败！</p>";
	}
	$num_result=$result->num_rows;
	
	if ($num_result <> 0) {
		for ($i=0; $i < $num_result; $i++) {
			$row=$result->fetch_assoc ();
			$rev_bef = stripslashes ($row['rev']);
			if ($rev_bef == $rev) {
				$rev = $rev_bef +1;
			}
	}
	}
	$result->free();
	
	$path_name = $file_name."_Rev.".$rev."_".$user_id.".pdf";
	
	//在这传文件
	//如果出错了，在这打住
	if ($_FILES['userfile']['error']>0) {
	echo 'Problem: ';
	switch ($_FILES['userfile']['error']) {
		case 1: echo 'File exceeded upload_max_filesize';
		break;
		case 2: echo 'File exceeded max_file_size';
		break;
		case 3: echo 'File only partially uploaded';
		break;
		case 4: echo 'No file uploaded';
		break;
		case 6: echo 'Cannot upload file: No temp directory specified';
		break;
		case 7: echo 'Upload failed: Cannot write to disk';
		break;
	}
	echo "</div>";
	echo "</body>";
	echo "</html>";
	exit;
	}	


	$ftp_config = array(
		'hostname' => 'localhost',
		'username' => 'bid',
		'password' => '121121',
		'port' => 21
	);

	$ftp = new Ftp();
	$ftp->connect($ftp_config);
	$ftp->upload($_FILES['userfile']['tmp_name'], $path_name);
	
	$issue_date = date ('Y-m-d');
	
	$ftp->close();
	
	//在files做好记录
	$query="insert into files (`name`, `rev`, `issuer`, `issue_date`, `task_id`, `type`, `subject`) value ('".$file_name."', '".$rev."', '".$user_id."', '".$issue_date."', '".$task_id."', '标书', '".$subject."')";
	
	$result=$db->query ($query);
	
	if (!$result) {
		echo "<p>在files中记录失败！</p>";
		echo "<p>".$file_name."/".$rev."/".$user_id."/".$issue_date."/".$subject."</p>";
	}
	
	$db->close();
	
	//列出编号基本信息
	echo "
		<br />
		<div class='row'>
			<div class='col-md-2'><p class='text-right'>报价编号：</p></div>
			<div class='col-md-2'><p>".$project_code."</p></div>
			<div class='col-md-2'><p class='text-right'>项目名称：</p></div>
			<div class='col-md-4'><p>".$project_name."</p></div>
			<div class='col-md-1'><p class='text-right'>销售：</p></div>
			<div class='col-md-1'><p>".$sale_name."</p></div>
		</div>
		<br />
		
	";
	
	
	
}
?>

<br />
<form action = "../PHP/issue_tproposal.php" method = "post" enctype="multipart/form-data">	
  <input type="hidden" name="MAX_FILE_SIZE" value="20000000" />
  <div class="form-group">
	<label for="userfile">上传文件：</label>
    <input type = "file" name = "userfile" id="userfile" />
	<p class="help-block">选择上传文件(合成一份pdf文件)</p>
  </div>
	<input type='hidden' name = 'project_id' value = '<?php echo $project_id; ?>'/>
	<input type='hidden' name = 'task_id' value = ' <?php echo $task_id; ?>'/>
	<input type='hidden' name = 'subject' value = '<?php echo $subject; ?>'/>
	<button class="btn btn-default" type = "submit">发送</button>
</form>

<br />
<br />
<p class="text-center lead">文件提交列表：</p>

<?php
//引用必要的函数
require_once ('../FNS/db_fns.php');


//连接数据库并搜索数据
$db= db_connect();

//把本专业该项目的澄清搜出来
//还需要获取提交人
$query="select files.id, files.name, files.issue_date, files.rev, files.check_date, files.check_result, files.issuer, a.name as issue_name, b.name as check_name from files 
	left join users as a on files.issuer = a.id 
	left join users as b on files.checker = b.id 
	where task_id ='".$task_id."' and issuer = '".$user_id."'";
$result=$db->query ($query);
if (!$result) {
	echo "</div>";
	echo "</body>";
	echo "</html>";
	exit;
}
//获取记录数量
$num_results=$result->num_rows;

//写表头
echo "<table class='table table-striped'>";
echo "<tr>";
	echo "<td align='center' class='info'><strong>文件名</strong></td>";
	echo "<td align='center' class='info'><strong>版本号</strong></td>";
	echo "<td align='center' class='info'><strong>提出人</strong></td>";
	echo "<td align='center' class='info'><strong>提出时间</strong></td>";
	echo "<td align='center' class='info'><strong>校核人</strong></td>";
	echo "<td align='center' class='info'><strong>校核时间</strong></td>";
	echo "<td align='center' class='info'><strong>校核结论</strong></td>";
echo "</tr>";



//循环编制表格
for ($i=0; $i < $num_results; $i++) {

	$row=$result->fetch_assoc ();
	
	$name = stripslashes ($row['name']);
	$rev = stripslashes ($row['rev']);
	$issuer = stripslashes ($row['issue_name']);
	$issue_date = stripslashes ($row['issue_date']);
	$checker = stripslashes ($row['check_name']);
	$check_date = stripslashes ($row['check_date']);
	$check_result = stripslashes ($row['check_result']);
	
	
	
	echo "<tr>";
    echo "<td align ='center'>".$name."</td>";
	echo "<td align ='center'>".$rev."</td>";
	echo "<td align ='center'>".$issuer."</td>";
	echo "<td align ='center'>".$issue_date."</td>";
	echo "<td align ='center'>".$checker."</td>";
	echo "<td align ='center'>".$check_date."</td>";
	echo "<td align ='center'>".$check_result."</td>";
	echo "</tr>";
	
}
echo "</table>";
$result->free();
$db->close();

?>

</div>
</body>
</html>
